package brightrac.database;

import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import brightrac.common.*;
public class Basic {	
	
	private String userName = null;
	private String password  = null;
	public String dataBaseName = null;
	public String host = null;
	private String url = "jdbc:mysql://";
	public Connection connector = null;	  
	
	public  PreparedStatement prepareStatement;
	public PrintStream OUT=System.out;
	//Set user name
	private void setUserName(String userName)
	{
		this.userName = userName;
	}
	//Set password
	private void setPassword(String password)
	{
		this.password = password;
	}
	// set DataBase
	private void setDatabaseName(String dataBaseName)
	{
		this.dataBaseName = dataBaseName;
	}
	// set host
	private void sethost(String host)
	{
		this.host = host;
	}
	//set url
	private void setUrl()
	{
		url = "jdbc:mysql://" + host + "/" + dataBaseName;
	}
	/**
	 * 
	 * @param host Mysql host
	 * @param dataBaseName Bug Database
	 * @param userName user name
	 * @param password user password
	 */
	public void setup(  String host, String dataBaseName, String userName, String password)
	{
		setUserName(userName);
		setPassword(password);
		setDatabaseName(dataBaseName);
		sethost(host);
		setUrl();
			
	}
	
	/**
	 *  Connect to Mysql Database
	 * @return {@link Result} 
	 */
	public Result connect()
	{
		try
		{
			connector = DriverManager.getConnection (url, userName, password);
			return( new Result (true, "Database connection established"));
		}
		catch (SQLException e)
		{
			return( new brightrac.common.Error ("Cannot connect to database server\n" + e.getMessage()));
		}
		
	}
	
	/**
	 * It give output of SQL Query.
	 * @param query Sql Query
	 * @return {@link QueryResult}
	 */
	public QueryResult getQueryResult(String query)
	{
		try 
		{
			prepareStatement = connector.prepareStatement(query);			
			ResultSet data = prepareStatement.executeQuery();
			return(new QueryResult(data));
		}
		catch (SQLException ex)
		{
			// handle any errors
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
			return(new QueryResult(ex.getMessage()));
		}
	}
	
	/**
	 * Its run SQL query like create, insert.
	 * @param query
	 * @return {@link QueryResult}
	 */
	public QueryResult runQuery(String query)
	{
		try 
		{
			prepareStatement = connector.prepareStatement(query);
			prepareStatement.execute(query);
		}
		catch (SQLException ex)
		{
			// handle any errors
			System.out.println("Query :" + query +":");
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
			return(new QueryResult(ex.getMessage()));
		}
		return (new QueryResult(true));
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Basic db = new Basic();
		db.setup( "174.129.226.150", "", "root",  "");
		System.out.println(db.connect().message);
		System.out.println(db.runQuery("CREATE TABLE example(id INT, data VARCHAR(100) )").status);
		System.out.println(db.runQuery("drop table example").status);
		
//		DBSetup createTable = new DBSetup(db);
//		createTable.createTables("dataSchema/sql_create.txt");
//		createTable.insertIntialData("dataSchema/sql_Insert.txt");
		

	}

}
